package com.gaswell.canal.impl;

import com.gaswell.canal.AbastractCanalOperator;
import com.gaswell.canal.Canalinterface;
import com.gaswell.mapper.AlarmRecordMapper;
import com.gaswell.mapper.DiagnosisNewMapper;
import com.gaswell.mapper.ParamsMapper;
import com.gaswell.pojo.AlarmRecord;
import com.gaswell.pojo.Diagnosis;
import com.gaswell.pojo.Params;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName NullCanalOperator
 * @Author xiaow
 * @DATE 2024/4/7 21:54
 * 数据失真，需要进行回溯处理
 **/
@Component(value = "distortionCanalOperator")
public class DistortionCanalOperator extends AbastractCanalOperator {
    @Autowired
    private DiagnosisNewMapper diagnosisNewMapper;

    @Autowired
    private AlarmRecordMapper alarmRecordMapper;
    @Autowired
    private ParamsMapper paramsMapper;

    @Override
    public void operate(Diagnosis diagnosis) {
//     寻找失真-追溯数
        int N = 0;
//                params    以jh和param_name进行匹配，去找到失真的追溯数
        Params params = paramsMapper.selectByNameaAndJh("失真-追溯数", diagnosis.getJh());
        if (params == null) {
//                    没有特定的设置，就选择paramjh为as_well
            N = (int) paramsMapper.selectByNameaAndJh("失真-追溯数", "all_well").getParam_value();
        } else {
//                    有特定的设置，就采用特定的值
            N = (int) params.getParam_value();
        }
//                失真报警数
        int M = 0;
        Params params1 = paramsMapper.selectByNameaAndJh("失真-报警数", diagnosis.getJh());
        if (params == null) {
            M = (int) paramsMapper.selectByNameaAndJh("失真-报警数", "all_well").getParam_value();
        } else {
            M = (int) params1.getParam_value();
        }


        List<String> dateList = diagnosisNewMapper.findDateList(N, diagnosis.getJh(), diagnosis.getCjsj());
        int distortionNum = 0;
        for (String s : dateList) {
//                查询每个时间点的所有数据
            List<Diagnosis> datas = diagnosisNewMapper.findDataByTime(diagnosis.getJh(), s);
            for (Diagnosis data : datas) {
                if (data.getCategory().equals("distortion"))
                    distortionNum++;
            }
        }

        if (distortionNum > M) {
            AlarmRecord alarmRecord = new AlarmRecord();
            alarmRecord.setTime(dateList.get(dateList.size() - 1));
            alarmRecord.setAlarm_category("数据失真");
            alarmRecord.setRecord("往前追溯" + String.valueOf(N) + "个点，数据失真达到" + String.valueOf(M) + "次");
            alarmRecord.setJh(diagnosis.getJh());
            alarmRecordMapper.insert(alarmRecord);
        }
    }
}
